package org.clxmm.security03springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author clxmm
 * @Description
 * @create 2021-06-07 8:32 下午
 */
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    // 定义用户信息服务 （查询用户信息）
//    @Override
//    @Bean
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
//        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("p1").build());
//        inMemoryUserDetailsManager.createUser(User.withUsername("ls").password("456").authorities("p2").build());
//        return inMemoryUserDetailsManager;
//    }


    //密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {

//        return NoOpPasswordEncoder.getInstance();
        return  new BCryptPasswordEncoder();
    }


    // 安全拦截机制
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.
                authorizeRequests()
                // /r/** 的请求需要认证
                .antMatchers("/r/r1").hasAnyAuthority("p1")
                .antMatchers("/r/r2").hasAnyAuthority("p2")
                .antMatchers("/r/**").authenticated()
                // 其他的都通过
                .anyRequest().permitAll()
                .and()
                // 允许表单登陆
                .formLogin()
                // 登陆页面配置
//                .loginPage("/login‐view")
//                // 指定登录处理的URL，也就是用户名、密码表单提交的目的路径
//                .loginProcessingUrl("/login")
                // 自定义登陆成功的页面地址
                .successForwardUrl("/login‐success")
                .and()
                // 退出配置
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login‐view?logout")
                .and()
                //屏蔽CSRF控制，即spring security不再限制CSRF
                .csrf().disable()
                //如果需要就创建一个Session(默认)登录时
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

                .and()
                .sessionManagement()
        ;
        http.sessionManagement()
                .invalidSessionUrl("/login‐view?error=INVALID_SESSION")
                .maximumSessions(1)
                .expiredUrl("/login‐view?error=EXPIRED_SESSION");



    }
}
